<?php

namespace api\web\controller;

use cmf\controller\RestBaseController;
use api\web\model\NewsModel;

class NewsController extends RestBaseController
{
    public function index()
    {
        $list = NewsModel::select();
        $this->success('院系新闻显示', $list);
    }
    public function getNews()
    {
        //获取搜索字段
        $data = input('data');
        $page = input('page/d', 1);
        $row = input('row/d', 10);
        $where = [];
        //判断是否有值
        if ($data) {
            $where[] = ['author|title', 'like', '%' . $data . '%'];
        }
        $list = NewsModel::order('order', 'DESC')
            ->field('id,title,author,image,abstract,look_num,create_time')
            ->where($where)
            ->page($page, $row)->select();
        $this->success('新闻列表', $list);
    }

    //显示内容
    public function Content()
    {
        $id = input('id/d');
        if (empty($id)) {
            $this->error('参数错误');
        }
        $news = NewsModel::withoutField('delete_time,order')->find($id);
        if (!$news) {
            $this->error('新闻不存在');
        }
        $content1 = htmlspecialchars_decode($news['content']);
        $content2 = cmf_replace_content_file_url($content1, true);
        $this->success('返回成功', $news);
    }
}